package servlet;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import po.PageCity;
import service.CityService;

/**
 * Create By IntelliJ IDEA
 *
 * @author: jsonor
 * @create-Time: 2017/10/23 13:56
 * @description: 每次提交到这个servlet的时候，就必须告诉我，你要查询第几页
 */
@WebServlet(name = "displayPageServlet", value = {"/displayPage"})
public class DisplayPageServlet extends HttpServlet {

  // 每一页的显示的条数
  private static int COUNT_PER_PAGE;
  // 分页时需要显示的控件数量
  private static int COUNT_PER_PAGE_ITEM;

  @Override
  public void init() throws ServletException {
    String countOfPage = getServletContext().getInitParameter("CountOfPage");
    COUNT_PER_PAGE = Integer.parseInt(countOfPage);

    String countOfItem = getServletContext().getInitParameter("CountOfItem");
    COUNT_PER_PAGE_ITEM = Integer.parseInt(countOfItem);
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    // 需要查询的页码
    int pageId = Integer.parseInt(req.getParameter("pageId"));
    CityService service = new CityService();
    PageCity pageCity = null;
    try {
      pageCity = service.findCityByPageId(pageId, COUNT_PER_PAGE, COUNT_PER_PAGE_ITEM);
      req.setAttribute("page", pageCity);
      req.getRequestDispatcher("/display_page.jsp").forward(req, resp);
    } catch (SQLException e) {
      e.printStackTrace();
    }

  }
}
